#include "srregisterwidget.h"
#include <QGridLayout>
#include <QPushButton>
#include <QLabel>
#include <QLineEdit>
#include <QRegExp>
#include <QRegExpValidator>
#include <QKeyEvent>
#include <QDebug>


// OE
#include <OEMessageBox>

SRRegisterWidget::SRRegisterWidget(QWidget *parent) : QWidget(parent)
{
    QWidget* wdgt_logo = new QWidget();
    QLabel* lab_logo = new QLabel();
    lab_logo->setAlignment(Qt::AlignCenter);
    lab_logo->setPixmap(QPixmap(":/lab/logo"));
    labLogoTitle_ = new QLabel("复审员注册");
    labLogoTitle_->setAlignment(Qt::AlignCenter);
    labLogoTitle_->setObjectName(QString("RegisterLogoTitle"));

    editName_ = new QLineEdit();
    QLabel* labName_ = new QLabel(tr("用户姓名"));

    editUser_ = new QLineEdit();
    QLabel* labUser_ = new QLabel(tr("用户账号"));

    editPswd_ = new QLineEdit();
    QLabel* labPswd_ = new QLabel(tr("登录密码"));

    editConfirmPswd_ = new QLineEdit();
    QLabel* labConfirmPswd_ = new QLabel(tr("确认密码"));

    btnRegister_ = new QPushButton(tr("确认注册"));


    //
    btnRegister_->setFixedSize(110,45);
    wdgt_logo->setFixedSize(130,140);
    editName_->setFixedHeight(30);
    editUser_->setFixedHeight(30);
    editPswd_->setFixedHeight(30);
    editConfirmPswd_->setFixedHeight(30);


    // 输入限制规则
    QRegExp regQuote("[0-9a-zA-Z]{1,12}");
    QRegExpValidator* quoteValidator = new QRegExpValidator(regQuote, this);

    QRegExp reg_cryptolalia("^[\u4e00-\u9fa5]{0,4}$");
    QRegExpValidator* validator_cryptolalia = new QRegExpValidator(reg_cryptolalia, this);

    // 属性设置
    editName_->setPlaceholderText(tr("请输入中文姓名"));
    editName_->setValidator(validator_cryptolalia);
    editUser_->setPlaceholderText(tr("请输入6 - 12位账号(英文或数字)"));
    editPswd_->setPlaceholderText(tr("请输入6 - 12位密码(英文或数字)"));
    editPswd_->setEchoMode(QLineEdit::Password);
    editConfirmPswd_->setPlaceholderText(tr("请确认密码"));
    editConfirmPswd_->setEchoMode(QLineEdit::Password);

    // 设置输入限制
    editUser_->setValidator(quoteValidator);
    editPswd_->setValidator(quoteValidator);
    editConfirmPswd_->setValidator(quoteValidator);

    // 布局
    QVBoxLayout* layout = new QVBoxLayout();
    QGridLayout* layout_logo = new QGridLayout();
    QHBoxLayout* layout_name = new QHBoxLayout();
    QHBoxLayout* layout_user = new QHBoxLayout();
    QHBoxLayout* layout_pswd = new QHBoxLayout();
    QHBoxLayout* layout_confirm_pswd = new QHBoxLayout();

    layout_logo->addWidget(lab_logo,0,1,1,1);
    layout_logo->addWidget(labLogoTitle_,1,1,1,1);
    layout_name->addWidget(labName_);
    layout_name->addWidget(editName_);
    layout_user->addWidget(labUser_);
    layout_user->addWidget(editUser_);
    layout_pswd->addWidget(labPswd_);
    layout_pswd->addWidget(editPswd_);
    layout_confirm_pswd->addWidget(labConfirmPswd_);
    layout_confirm_pswd->addWidget(editConfirmPswd_);

    wdgt_logo->setLayout(layout_logo);
    layout->addWidget(wdgt_logo,0,Qt::AlignHCenter);
    layout->addLayout(layout_name);
    layout->addLayout(layout_user);
    layout->addLayout(layout_pswd);
    layout->addLayout(layout_confirm_pswd);
    layout->addWidget(btnRegister_,0,Qt::AlignHCenter);

    setLayout(layout);

//    setMinimumSize(320,380);
//    setFixedSize(320,380);

    // 信号链接
    connect(btnRegister_,&QPushButton::clicked,
            this,[this](){
        if (this->editName_->text().isEmpty()) {
            OEMessageBox::BriefMessage(this,tr("姓名不能为空"));
            return;
        }
        else if (this->editUser_->text().size() < 6
              || this->editConfirmPswd_->text().size() < 6){
            OEMessageBox::BriefMessage(this,tr("账号密码不能短于6位"));
            return;
        }
        else if (this->editPswd_->text()
                != this->editConfirmPswd_->text()) {
            OEMessageBox::BriefMessage(this,tr("密码不一致"));
            return;
        }

        emit this->sigRegister(this->editName_->text(),
                               this->editUser_->text(),
                               this->editPswd_->text());

    });
}

SRRegisterWidget::~SRRegisterWidget()
{
#ifdef QT_DEBUG
    qDebug() << "~账户注册窗口被析构";
#endif
}

void SRRegisterWidget::setRegisterMode(int _mode)
{
    if (_mode == 0) {
        labLogoTitle_->setText(tr("复审员注册"));
    }
    else {
        labLogoTitle_->setText(tr("重新注册复审员"));
    }
}


void SRRegisterWidget::keyPressEvent(QKeyEvent *event) {
    if (event->key() == Qt::Key_Enter || event->key() == Qt::Key_Return) {

        event->accept();
    }
    else if (event->key() == Qt::Key_Escape) {

        event->accept();
    }
}
